package luanluan.control.buttons 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	 * 单选按钮组
	 * @author luanluan
	 */
	
	public class LRadioGroup extends EventDispatcher 
	{
		//	*所有选项
		private var _itemList:Vector.<LRadioButton> = new Vector.<LRadioButton>();	
		//	*当前选项
		private var _selectItem:LRadioButton;
		
		//	*是否可用
		private var _enabled:Boolean = false;
		
		/**
		 * 单选按钮组
		 */
		public function LRadioGroup() 
		{
			//构造函数
		}
		
		/**
		 * 添加选项
		 * @param	item	待添加的单选按钮
		 */
		public function addItem(item:LRadioButton):void
		{
			if (_itemList.indexOf(item) == -1)
			{
				_itemList[_itemList.length] = item;
				item.group = this;
			}
		}
		
		/**
		 * 根据id改变当前选择选项
		 * @param	itemID		选项ID，按addItem()添加顺序，从0开始
		 * @param	sendEvent	是否发送Event.CHANGE事件
		 */
		public function changeItemByID(itemID:int,sendEvent:Boolean = false):void
		{
			changeItem(_itemList[itemID], sendEvent);
		}
		
		//	*选项被选处理
		internal function itemChangeHandle(item:LRadioButton):void
		{
			changeItem(item, true);
		}
		
		//	*改变所选项
		private function changeItem(item:LRadioButton, sendEvent:Boolean = true):void
		{
			if(_selectItem)
			{
				if (_selectItem == item)
				{
					_selectItem.selected = true;
					return;
				}
					
				_selectItem.selected = false;
			}
				
			_selectItem = item;
			_selectItem.selected = true;
			
			if (sendEvent)
				dispatchEvent(new Event(Event.CHANGE));
		}
		
		//	*启用
		private function addListener():void
		{
			for each (var item:LRadioButton in _itemList) 
			{
				item.enabled = true;
			}
		}
		
		//	*禁用
		private function removeListener():void
		{
			for each (var item:LRadioButton in _itemList) 
			{
				item.enabled = false;
			}
		}
		
		//	*是否启用
		public function get enabled():Boolean 						{			return _enabled;		}
		
		//	*设置启用
		public function set enabled(value:Boolean):void 
		{
			if (_enabled == value)
				return;
				
			_enabled = value;
			if (_enabled)
				addListener();
			else
				removeListener();
		}
		
		//	*所有选项
		public function get itemList():Vector.<LRadioButton> 		{			return _itemList;		}
		
		//	*所选选项
		public function get selectItem():LRadioButton 				{			return _selectItem;		}
		
		//	*所选选项ID
		public function get selectID():int							{			return _itemList.indexOf(_selectItem);		}
		
	}

}